﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// include spécifique au projet
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace rx_reseau
{
    public partial class MainWindow : Form
    {
        delegate void dSetText(string myString);
        Thread t;
        Socket socket;
        bool m_fin;
        double MontantDepart;
        string trame;
        double[,] nombreMontant = {{100, 50, 20, 10, 5, 2, 1, 0.25, 0.10, 0.05, 0.01},
                                      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
        //int Hundred, Fifty, Vingt, Ten, Cinq, Deux, Un, trentesous, dix, cinq, noir;
        public MainWindow()
        {
            InitializeComponent();
            m_fin = false;
            MontantDepart = 0.0;
            trame = "";
        }

        /// <summary>
        /// Démarre un thread pour écouter sur un port
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRecevoir_Click(object sender, EventArgs e)
        {
            m_fin = false;
            t = new Thread(new ThreadStart(Thread_RX));
            t.Start();
        }

        /// <summary>
        /// Sert à construire la trame qui sera envoyée.
        /// </summary>
        private void monteTrame()
        {
            int nombre = 0;
            for (int i = 0; i < 11; i++)
            {
                nombre = (int)nombreMontant[1, i];
                trame += nombre.ToString();
                trame += ";";
            }
            trame.Remove(trame.Length - 1);
        }

        private void testMontant()
        {
            int index = 0;
            while (index < 11)
            {
                if (MontantDepart >= nombreMontant[0, index])
                {
                        MontantDepart -= nombreMontant[0, index];
                        nombreMontant[1, index] += 1;
                }
                else
                    index++;
            }
        }
       
        private void Thread_RX()
        {
            int nPortRX = 5001;
            int nRecu = 0;
            EndPoint PointLocal = new IPEndPoint(0, nPortRX);
            EndPoint PointDistant = new IPEndPoint(0, 0);
            byte[] bTexte = new byte[10000];
            string strText;

            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.Bind(PointLocal);
                do
                {
                    if (socket.Available > 0)
                    {
                        nRecu = socket.ReceiveFrom(bTexte, ref PointDistant);
                        strText = Encoding.ASCII.GetString(bTexte).Substring(0, nRecu);
                        MontantDepart = Convert.ToDouble(strText);
                        testMontant();
                        monteTrame();
                        strText = "";
                        bTexte = Encoding.ASCII.GetBytes(trame);
                        socket.SendTo(bTexte, PointDistant);
                        // Clean tableau
                        for (int i = 0; i < 11; i++)
                        {
                            nombreMontant[1, i] = 0;
                        }
                        // Reset valeur
                        trame = "";
                        MontantDepart = 0;
                    }
                }
                while(m_fin==false);
            }
            catch (SocketException se)
            {
                // MessageBox.Show(se.Message);
            }
        }

        private void AfficheTexte(string str)
        {
            // Ne sert plus dans cette version. Se référer p.56 "Réseau et Linux"
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            m_fin = true;
            // if (t.IsAlive)
               // t.Abort();

            socket.Close();
        }
    }
}
